package com.factors.ZhangDingHou.Parental_Education_Level;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class ExamScoreFilterMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private static final IntWritable one = new IntWritable(1);
    private Text parentalEducationLevel = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        if (key.get() == 0) return; // Skip header line

        String[] columns = value.toString().split(",");
        if (columns.length >= 20) {
            try {
                double examScore = Double.parseDouble(columns[19]); // Exam_Score is the 20th column
                if (examScore > 80) {
                    String educationLevel = columns[16]; // Parental_Education_Level is the 17th column

                    // Emit a tuple of parental education level and count for each row with Exam_Score > 80
                    parentalEducationLevel.set(educationLevel);
                    context.write(parentalEducationLevel, one);
                }
            } catch (NumberFormatException e) {
                // Handle parsing errors or non-numeric values
            }
        }
    }
}